﻿using SoccerEvaluator.Common;
using System;
using System.Collections.Generic;

using System.Linq;
using System.Web;

namespace SoccerEvaluator.WebSite.App_Code
{
    public class ObjectFactory
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        internal static TemplateInfo ToTemplateInfo(Template t)
        {
            TemplateInfo item = new TemplateInfo();

            item.ID = t.Id;
            item.Name = t.Name;
            item.Logo = t.Image;

            //item.AccessCode=t.
            item.CatchScore = t.CatchScore;
            //item.EnableUpdates=t.en
            item.EndDate = t.EndDate;
            item.ExactScore = t.ExactScore;
            item.ProjectMode = t.Mode;
            //item.RegisterDate=t.re
            item.StartDate = t.StartDate;
            //item.TableResults
            //item.Total=t.to
            //item.UrlScores=
            //item.User=t.u
            //item.UserID=t.us

            item.Scores = ToTemplateScoreList(t.TemplateScores.ToList());

            return item;
        }

        internal static List<TemplateScoreInfo> ToTemplateScoreList(List<TemplateScore> dbScores)
        {
            List<TemplateScoreInfo> list = new List<TemplateScoreInfo>();

            dbScores.ForEach(delegate(TemplateScore t)
            {
                TemplateScoreInfo s = ToTemplateScoreInfo(t);

                list.Add(s);

            });

            return list;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        internal static List<TemplateInfo> ToTemplateInfoList(List<Template> dbResults)
        {
            List<TemplateInfo> list = new List<TemplateInfo>();

            dbResults.ForEach(delegate(Template t)
            {
                TemplateInfo newResult = ToTemplateInfo(t);

                list.Add(newResult);
            });

            return list;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        internal static TemplateScoreInfo ToTemplateScoreInfo(TemplateScore t)
        {
            TemplateScoreInfo item = new TemplateScoreInfo();

            item.Date = t.Date;
            item.Index = t.Index;
            item.Location = t.Location;
            item.Local = t.Local;
            item.LocalGoals = t.LocalGoals;
            
            item.TemplateId = t.TemplateId;
            item.Visitor = t.Visitor;
            item.VisitorGoals = t.VisitorGoals;

            return item;
        }
        internal static TemplateScoreInfo ToTemplateScoreInfo(UserScore t)
        {
            TemplateScoreInfo item = new TemplateScoreInfo();

            item.Date = t.Date;
            item.Index = t.Index;            
            item.LocalGoals = t.LocalGoals;
            
            item.TemplateId = t.TemplateId;
            
            item.VisitorGoals = t.VisitorGoals;

            return item;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dbuser"></param>
        /// <returns></returns>
        internal static SystemUserInfo ToSystemUserInfo(SystemUser dbuser)
        {
            SystemUserInfo item = new SystemUserInfo();

            item.Email = dbuser.Email;
            item.LastAccess = dbuser.LastEntry;
            item.Name = dbuser.Name;
            item.Password = dbuser.Password;
            item.RegistryDate = dbuser.CreationDate;
            item.UserId = dbuser.Id;
            item.Username = dbuser.UserName;

            return item;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dbGroups"></param>
        /// <returns></returns>
        internal static List<GroupInfo> ToGroupsList(List<Group> dbGroups)
        {
            List<GroupInfo> list = new List<GroupInfo>();

            dbGroups.ForEach(delegate(Group t)
            {
                GroupInfo newResult = ToGroupsInfo(t);

                list.Add(newResult);
            });

            return list;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dbGroup"></param>
        /// <returns></returns>
        internal static GroupInfo ToGroupsInfo(Group dbGroup)
        {
            GroupInfo item = new GroupInfo();

            item.Id = dbGroup.Id;
            item.Name = dbGroup.Name;
            item.Logo = dbGroup.Logo;

            return item;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dbScores"></param>
        /// <param name="dbTemplate"></param>
        /// <returns></returns>
        internal static List<UserScoreInfo> ToUserScoreInfoList(List<UserScore> dbScores, TemplateScore dbTemplate)
        {
            List<UserScoreInfo> list = new List<UserScoreInfo>();

            dbScores.ForEach(delegate(UserScore t)
            {
                UserScoreInfo s = ToUserScoreInfo(t, dbTemplate);

                list.Add(s);

            });

            return list;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="u"></param>
        /// <param name="t"></param>
        /// <returns></returns>
        public static UserScoreInfo ToUserScoreInfo(UserScore u, TemplateScore t)
        {
            UserScoreInfo item = new UserScoreInfo();

            //Datos de usuario
            item.TemplateId = u.TemplateId;
            item.Date = u.Date;
            item.Index = u.Index;
            item.LocalGoals = u.LocalGoals;
            item.VisitorGoals = u.VisitorGoals;

            //Datos de plantilla
            item.Local = t.Local;
            item.Location = t.Location;
            item.Visitor = t.Visitor;

            item.Score = u.Score;
            item.UserName = u.SystemUser.Name;

            return item;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dbUserScores"></param>
        /// <returns></returns>
        internal static List<TemplateScoreInfo> ToTemplateScoreInfoList(List<UserScore> dbUserScores)
        {
            List<TemplateScoreInfo> list = new List<TemplateScoreInfo>();


            dbUserScores.ForEach(delegate(UserScore x)
            {
                TemplateScoreInfo t = ToTemplateScoreInfo(x);

                list.Add(t);
            });

            return list;
        }
    }
}